package com.jack.payment.util.serializer;

import com.jack.payment.annotation.BigDecimalRange;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.math.BigDecimal;

/**
 * BigDecimal 金额交易验证
 */
public class BigDecimalVaildator implements ConstraintValidator<BigDecimalRange, BigDecimal> {
    private BigDecimal min;
    private BigDecimal max;

    @Override
    public void initialize(BigDecimalRange constraintAnnotation) {
        min = new BigDecimal(constraintAnnotation.min());
        max = new BigDecimal(constraintAnnotation.max());
    }

    @Override
    public boolean isValid(BigDecimal value, ConstraintValidatorContext constraintValidatorContext) {
        if (value == null) {
            return true; // or false, depending on your requirements
        }
        return value.compareTo(min) >= 0 && value.compareTo(max) <= 0;
    }
}
